// pages/distributionOrder/distributionOrder.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    integralList: [], //记录详情容器
    loading: true, //true显示， flase隐藏
    pagesize: 5, //每页数据量
    page: 0, //页数
    loading: true, //加载中图标显示隐藏
    nomore: false, //没有更多提示
    noData: true,
    emptyTip: {
      font: "您暂时还没有订单哦~",
      imgUrl: "https://try.0791jr.com/WeChat/images/empty-korder.png"
    },
  },

  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
    this.loadMore();  //初始化加载
  },

  onReachBottom: function () {
    if (this.data.noData) {
      return;
    }
    this.data.nomore || this.loadMore();  //上拉加载
  },

  //获取更多
  loadMore(cd) {
    this.setData({
      loading: true,
      page: this.data.page + 1
    })
    //请求商品列表数据
    wx.request({
      url: app.baseURL + 'Distribution&a=order',
      data: {
        user_id: app.globalData.userId,
        size: this.data.pagesize,  //每页数据量
        page: this.data.page,  //页数
      },
      success: (res) => {
        if (res.statusCode == 200) {
          if (res.data.status == 2){
            this.setData({loading: false});
            wx.showModal({
              title: '温馨提示',
              content: "你还没有下级分销商哦，快去邀请好友成为你的分销商吧！",
              success: (res) => {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../invitation/invitation',
                  })
                }
              }
            })
          }else{
            const data = this.data;
            //更新数组内容
            data.integralList = [...data.integralList, ...res.data.order];
            this.setData({
              info: data.integralList,
              noData: data.integralList.length > 0 ? false : true,  //判断是否为空
              nomore: res.data.order.length < this.data.pagesize,  //判断是否已经加载到了最后一页
              loading: false,
            });
          };
          typeof cb == 'function' && cb();
        }
      },
      'fail': (res) => {
        wx.showToast({ title: '网络错误，请稍后重试', icon: 'none' })
      }
    })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.setData({ integralList: [],page: 0})
    this.loadMore(wx.stopPullDownRefresh());  //初始化加载
  }
})